package com.gzcss.gulimall.ware.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.gzcss.common.utils.R;
import com.gzcss.gulimall.product.entity.SkuInfoEntity;
import com.gzcss.gulimall.ware.feign.ProductFeignService;
import com.gzcss.gulimall.ware.vo.SkuHasStock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzcss.common.utils.PageUtils;
import com.gzcss.common.utils.Query;

import com.gzcss.gulimall.ware.dao.WareSkuDao;
import com.gzcss.gulimall.ware.entity.WareSkuEntity;
import com.gzcss.gulimall.ware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
@Slf4j
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * skuId:
         * wareId: 1
         */
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            wrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            wrapper.eq("ware_id",wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(wareSkuEntities == null || wareSkuEntities.size() == 0){
            //如果库存中不存在商品则新增
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //todo 远程查询sku名字,如果失败，事务无需回滚
            //1、用try处理异常，这样事务就不会回滚
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                if(info.getCode() == 0){
                    wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
                }
            }catch (Exception e){

            }
            wareSkuDao.insert(wareSkuEntity);
        }else{
            //库存中有商品则更新
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }


    }

    @Override
    public List<SkuHasStock> hasStock(List<Long> skuIds) {

        List<SkuHasStock> collect = skuIds.stream().map(skuid -> {
            SkuHasStock vo = new SkuHasStock();
            Long count = baseMapper.hasStock(skuid);
            vo.setSkuId(skuid);
            vo.setHasStock(count==null?false:count > 0);
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

}
